package com.sanhe.rfmuscle.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSeekBar;

import com.sanhe.rfmuscle.R;

public class SlantedSeekBar extends AppCompatSeekBar {

    private Paint progressPaint;
    private Path progressPath;
    private int progressColor = Color.parseColor("#0EADF6"); // 默认进度颜色
    private int currentProgress;
    private int slantWidth; // 斜角宽度（像素）
    private int strokeWidth; // 描边宽度（像素）
    private int slantHeight; // 进度条高度（像素）
    private int slantPadding;

    private Path clipPath = new Path();

    public SlantedSeekBar(Context context) {
        super(context);
        init(context, null);
    }

    public SlantedSeekBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SlantedSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        // 从XML属性获取值
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlantedSeekBar);
            slantHeight = ta.getDimensionPixelSize(
                    R.styleable.SlantedSeekBar_slanted_height, 33);
            strokeWidth = ta.getDimensionPixelSize(
                    R.styleable.SlantedSeekBar_slanted_stroke_width, 15);
            progressColor = ta.getColor(
                    R.styleable.SlantedSeekBar_slanted_color, progressColor);
            setMax(ta.getInt(
                    R.styleable.SlantedSeekBar_slanted_max, 100));
            currentProgress = ta.getInt(
                    R.styleable.SlantedSeekBar_slanted_progress, 20);
            slantWidth = ta.getDimensionPixelSize(
                    R.styleable.SlantedSeekBar_slanted_slant_width, 5);
            slantPadding = ta.getDimensionPixelSize(
                    R.styleable.SlantedSeekBar_slanted_padding, 5);
            ta.recycle();
        }

        // 禁用系统绘制
        setSplitTrack(false);
        setWillNotDraw(false);


        // 初始化进度画笔
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setColor(progressColor);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);

        // 初始化路径
        progressPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = slantHeight;
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // 获取绘制区域
        int width = getWidth() - slantPadding * 2;
        int height = slantHeight;
        int top = getTop();
        int left = getLeft() + slantPadding;


        int centerY = height / 2;


        progressPath.reset();
        progressPath.moveTo(left, top + centerY);
        progressPath.lineTo(left + width, top + centerY);
        // 计算当前进度对应的宽度
        float progressWidth = ((float) currentProgress / getMax()) * width;
        // 保存画布状态
        canvas.save();

        if (currentProgress != getMax()){
            // 裁剪进度区域（右侧倾斜）
            clipPath.moveTo(getLeft(), top + height);
            clipPath.lineTo(getLeft(), top);
            clipPath.lineTo(progressWidth + slantWidth + slantPadding, top);
            clipPath.lineTo(progressWidth - slantWidth + slantPadding, top + height);
            clipPath.close();
            canvas.clipPath(clipPath);
            clipPath.reset();
        }


        if (currentProgress != 0){
            // 绘制进度条
            canvas.drawPath(progressPath, progressPaint);
        }

        // 恢复画布状态
        canvas.restore();
    }


    // 设置当前进度
    public void setProgress(int progress) {
        this.currentProgress = Math.min(getMax(), Math.max(0, progress));
        invalidate();
    }

    public int getProgress() {
        return currentProgress;
    }
}